package com.baizhi;

import com.baizhi.service.OrderItemService;
import com.baizhi.service.impl.OrderItemServiceImpl;
import org.apache.struts2.ServletActionContext;

import java.util.*;

public class OrderAction {
    private OrderItemService  os=new OrderItemServiceImpl();
    private List<Order> orderList;
    private int id;
    private int out_trade_no;

    private Order order;
    public String insertOrder(){
        Cart cart=(Cart)ServletActionContext.getRequest().getSession().getAttribute("cart");
        int uid =(int) ServletActionContext.getRequest().getSession().getAttribute("uid");
        double money = cart.getTotalMoney();
        order.setUserId(uid);
        order.setGoodPrice(money);
        System.out.println(order.getId());
        Map<Integer, CartItem> buyMap = cart.getBuyMap();
        List<OrderItem> orderItems=new ArrayList<>();
        Collection<CartItem> values = buyMap.values();
        for (CartItem value : values) {
            OrderItem  o=new OrderItem();
            o.setCount(value.getCount());
            o.setProductId(value.getBook().getId());
            double v = value.getBook().getDangPrice() * value.getCount();
            o.setAmount(v);
            orderItems.add(o);
        }
        os.Order(order,orderItems);
        ServletActionContext.getRequest().getSession().removeAttribute("cart");
        ServletActionContext.getRequest().getSession().removeAttribute("c");
        return "sucess";
    }

    public String selectMyOrder(){
        int uid =(int) ServletActionContext.getRequest().getSession().getAttribute("uid");
        orderList=os.selectMyOrders(uid);
        return "toList";
    }
    public String updateState(){
        boolean b = os.updateState(out_trade_no);
        return "ss";
    }

    public int getOut_trade_no() {
        return out_trade_no;
    }

    public void setOut_trade_no(int out_trade_no) {
        this.out_trade_no = out_trade_no;
    }

    public List<Order> getOrderList() {
        return orderList;
    }

    public void setOrderList(List<Order> orderList) {
        this.orderList = orderList;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }
}
